package flca.mda.api.util;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import mda.annotation.crud.Search;
import mda.type.IDtoType;
import mda.type.IEntityType;
import webapp.Tid;
import flca.mda.codegen.helpers.StrUtil;
import flca.mda.common.api.helpers.ImportHelper;

public class WebappIntferfaceUtils extends InterfaceUtils
{
	private WebappTypeUtils tu = new WebappTypeUtils();
	private FlexTypeUtils flexTypeutil = new FlexTypeUtils();
//	private NameUtils nu = new NameUtils();
	
	//this is map between service interface and all its crud methods. 
	protected Map<Class<?>, List<CrudMethod>> crudsMap = null;
	
	@Override
	public boolean moreParameters(Method aMethod, int aIndex)
	{
		int cnt = getParameterCount(aMethod);
		return (aIndex < cnt-1);
	}
	
	
	/**
	 * @param aField
	 * @return
	 */
	public Properties getSubsWithsForCrudOper(CrudMethod aCrudMethod, boolean aForFlex)
	{
		Class<?> crudent = aCrudMethod.getEntity(); 

		String entityName = tplu.getClassName(crudent, Tid.ENTITY.name()); 
		String pktype = (aForFlex) ? flexTypeutil.getIdTypeName(crudent) : tu.getIdTypeName(crudent);
		String validator = (aForFlex) ? "" : tplu.getClassName(crudent, Tid.ENTITY_VALIDATOR.name()); 
		String dao = (aForFlex) ? "" : tplu.getClassName(crudent, Tid.ENTITY_DAO.name());
		String daoImpl = (aForFlex) ? "" : tplu.getClassName(crudent, Tid.ENTITY_DAO_IMPL.name());
		String daosrvProp = (aForFlex) ? "" : "dao" + tplu.getClassName(crudent, Tid.ENTITY.name());
		String mock = (aForFlex) ? "" : tplu.getClassName(crudent, Tid.ENTITY_MOCK.name()); 
		String searchArgsDto =  getSearchArgsDtoName(crudent);
			
		Properties result = new Properties();
		result.put("entity", entityName);
		result.put("pkType", pktype);
		result.put("validator", validator);
		result.put("dao", dao);
		result.put("daoImpl", daoImpl);
		result.put("daosrvProp", daosrvProp);
		result.put("mock", mock);
		result.put("searchDto", searchArgsDto);
		
		return result;
	}
	
	/**
	 * return boolean to indicate if current class is of ttype IEntityType
	 * @return
	 */
	public boolean isEntity() 
	{
		Class<?> currcls = JetArgument.getCurrent().getElementClass();
		return isEntity(currcls);
	}

	/**
	 * return boolean to indicate if current class is of ttype IDtoType
	 * @return
	 */
	public boolean isDto() 
	{
		Class<?> currcls = JetArgument.getCurrent().getElementClass();
		return isDto(currcls);
	}
	
	/**
	 * return boolean to indicate if current class is of ttype IEntityType
	 * @return
	 */
	public boolean isEntity(Class<?> aClass) 
	{
		return (tu.hasType(aClass, IEntityType.class));
	}

	/**IDtoType
	 * return boolean to indicate if current class is of ttype IDtoType
	 * @return
	 */
	public boolean isDto(Class<?> aClass) 
	{
		return (tu.hasType(aClass, IDtoType.class));
	}
	

	public String getSearchArgsDtoName(Class<?> crudent) 
	{
		String result = "";
		
		// hmm, can we improve this?
		result = "Search" + crudent.getSimpleName() + "Dto";
		
		if (tu.hasAnnotation(crudent, Search.class)) {
			String pck = StrUtil.getPackage(crudent.getName());
			ImportHelper.addImport(pck + "." + result);
		}
		
		return result;
	}
	
}
